﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using CMS.BLL;
using CMS.BLL.Entity;
using Component.Worker.RecursiveProcess;
public partial class Controls_Products_ctrSearch : System.Web.UI.UserControl
{
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    private int nCurrentLanguage = Support.GetCurrentLanguage ();
    protected const string VERTICAL_COLUMN_NAME = "VerticalSort";
    protected const string HOLIZOTAL_COLUMN_NAME = "HolizotalSort";


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitData();
            this.BindDataToTrademarkDropdown();
            this.BindDataToCategoryDropdown();
            this.BindDataToTypeDropdown();
        }
    }

    private void InitData()
    {
        switch (nCurrentLanguage)
        { 
            case 1:
                lnkSearch.ImageUrl = "~/Images/search_btn_vn.gif";
                break;
            case 2:
                lnkSearch.ImageUrl = "~/Images/search_btn.gif";
                break;
            case 3:
                lnkSearch.ImageUrl = "~/Images/search_btn.gif";
                break;
        }
    }
    private void BindDataToTrademarkDropdown()
    {
        string sShow = "";
        switch (nCurrentLanguage)
        {
            case 1:
                sShow = "Chọn hãng";
                break;
            case 2:
                sShow = "Trademark";
                break;
            case 3:
                sShow = "Trademark";
                break;
        }
        DataTable dtb = new DataTable();
        ProductTradeMark obj = new ProductTradeMark();

        dtb = obj.Search();
        ddlTradeMark.DataSource = dtb;
        ddlTradeMark.DataValueField = "ProductTradeMarkID";
        ddlTradeMark.DataTextField = "TradeMarkName";
        ddlTradeMark.DataBind();
        Utl.SetNonSelectToControl(ddlTradeMark, sShow, "------ Rỗng ------");
    }
    private void BindDataToCategoryDropdown()
    {
        int nDefaultLang = Support.GetDefaultLanguage();
        int nCurentLang = Support.GetCurrentLanguage();
        DataTable mCateData = new DataTable();
        ddlCategory.Items.Clear();
        DataTable objTable = new DataTable();
        ProductCategory objProductCategory = new ProductCategory();
        objTable = objProductCategory.Search();
        int nCount = objTable.Rows.Count;
        FlowCategory flow = new FlowCategory(objTable, VERTICAL_COLUMN_NAME, HOLIZOTAL_COLUMN_NAME);
        flow.IDColumnName = "ProductCategoryID";
        flow.ParentIDColumName = "[Parent]";
        switch (nCurentLang)
        {
            case 1:
                flow.SortColumnName = "ProductCategoryName1";
                break;
            case 2:
                flow.SortColumnName = "ProductCategoryName2";
                break;
            case 3:
                flow.SortColumnName = "ProductCategoryName3";
                break;
        }

        flow.ParentSortDirection = "ASC";
        if (flow.Execute(0))
        {
            mCateData = flow.DestinationData();
        }
        if (mCateData != null)
        {
            foreach (DataRow dr in mCateData.Rows)
            {
                if (dr != null)
                {
                    string sText = string.Empty;
                    switch (nCurentLang)
                    {
                        case 1:
                            sText = Convert.ToString(dr["ProductCategoryName1"]);
                            break;
                        case 2:
                            sText = Convert.ToString(dr["ProductCategoryName2"]);
                            break;
                        case 3:
                            sText = Convert.ToString(dr["ProductCategoryName3"]);
                            break;
                    }
                    Int16 iHolz = Convert.ToInt16(dr[HOLIZOTAL_COLUMN_NAME]);
                    textCreateMarginLeft(ref sText, iHolz);
                    ListItem item = new ListItem(sText, Convert.ToString(dr["ProductCategoryID"]));
                    ddlCategory.Items.Add(item);
                }
            }            
        }
   //     ddlCategory.Items.Insert(0, new ListItem(MultipleLanguage.SELECTPRO[nCurentLang -1], "0"));
    }
    private void textCreateMarginLeft(ref string text, Int16 count)
    {
        if (count > 0)
        {
            string sMargin = String.Empty;
            for (int i = 0; i < count; i++)
            {
                sMargin += "--";
                sMargin += " ";
            }
            text = sMargin + text;
        }
    }
    private void BindDataToTypeDropdown()
    {
        string sShow = "";
        switch (nCurrentLanguage)
        {
            case 1:
                sShow = "Chọn Lĩnh vực";
                break;
            case 2:
                sShow = "Select field";
                break;
            case 3:
                sShow = "Select field";
                break;
        }
        DataTable dtb = new DataTable();
        ProductCategoryType obj = new ProductCategoryType();

        dtb = obj.Search();
        ddlType.DataSource = dtb;
        ddlType.DataValueField = "ProductCategoryTypeID";
        switch (nCurrentLanguage)
        { 
            case 1:
                ddlType.DataTextField = "Description1";
                break;
            case 2:
                ddlType.DataTextField = "Description2";
                break;
            case 3:
                ddlType.DataTextField = "Description3";
                break;
        }
        
        ddlType.DataBind();
        Utl.SetNonSelectToControl(ddlType, sShow, "------ Rỗng ------");
    }

    protected void lnkSearch_Click(object sender, ImageClickEventArgs e)
    {
        //Config.ROOT + Pages.FrontEnds.PRODUCT_CATEGORY

        string sLink = Config.ROOT + Pages.FrontEnds.PRODUCT_CATEGORY + "?" + Config.PRO_CAT + "=" + ConvertData.ConvertToString(ddlCategory.SelectedValue)
            + "&" + Config.TRADE_MARK + "=" + ConvertData.ConvertToString(ddlTradeMark.SelectedValue) 
            + "&" + Config.KEYWORD + "=" + ConvertData.ConvertStringToDB(txtKeyword.Text.Trim()) 
            + "&" + Config.TYPE + "=" + ConvertData.ConvertToString(ddlType.SelectedValue);
        Response.Redirect(sLink); 
    }
}